/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cn.edu.thu.laud.objectstore.index;

import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.cassandra.thrift.Column;
import org.apache.cassandra.thrift.ColumnOrSuperColumn;
import org.apache.cassandra.thrift.Deletion;
import org.apache.cassandra.thrift.Mutation;
import org.apache.cassandra.thrift.SuperColumn;

/**
 * 
 * @author zhuoan
 *
 */
public class BatchMutation
{
  private final Map<ByteBuffer, Map<String, List<Mutation>>> mutationMap;

  public BatchMutation()
  {
    this.mutationMap = new HashMap<ByteBuffer, Map<String, List<Mutation>>>();
  }

  private BatchMutation(Map<ByteBuffer, Map<String, List<Mutation>>> mutationMap)
  {
    this.mutationMap = mutationMap;
  }

  public BatchMutation addInsertion(ByteBuffer key, List<String> columnFamilies, Column column)
  {
    Mutation mutation = new Mutation();
    mutation.setColumn_or_supercolumn(new ColumnOrSuperColumn().setColumn(column));

    addMutation(key, columnFamilies, mutation);
    return this;
  }

  public BatchMutation addSuperInsertion(ByteBuffer key, List<String> columnFamilies, SuperColumn superColumn)
  {
    Mutation mutation = new Mutation();
    mutation.setColumn_or_supercolumn(new ColumnOrSuperColumn().setSuper_column(superColumn));

    addMutation(key, columnFamilies, mutation);
    return this;
  }

  public BatchMutation addDeletion(ByteBuffer key, List<String> columnFamilies, Deletion deletion)
  {
    Mutation mutation = new Mutation();
    mutation.setDeletion(deletion);
    addMutation(key, columnFamilies, mutation);
    return this;
  }

  private void addMutation(ByteBuffer key, List<String> columnFamilies, Mutation mutation)
  {
    Map<String, List<Mutation>> innerMutationMap = getInnerMutationMap(key);
    for (String columnFamily : columnFamilies)
    {
      if (innerMutationMap.get(columnFamily) == null)
      {
        innerMutationMap.put(columnFamily, Arrays.asList(new Mutation[] { mutation }));
      }
      else {
        List<Mutation> mutations = new ArrayList<Mutation>(innerMutationMap.get(columnFamily));

        mutations.add(mutation);
        innerMutationMap.put(columnFamily, mutations);
      }
    }
    this.mutationMap.put(key, innerMutationMap);
  }

  private Map<String, List<Mutation>> getInnerMutationMap(ByteBuffer key)
  {
    Map<String, List<Mutation>> innerMutationMap = this.mutationMap.get(key);
    if (innerMutationMap == null)
    {
      innerMutationMap = new HashMap<String, List<Mutation>>();
    }
    return innerMutationMap;
  }

  public Map<ByteBuffer, Map<String, List<Mutation>>> getMutationMap()
  {
    return this.mutationMap;
  }

  public BatchMutation makeCopy()
  {
    return new BatchMutation(this.mutationMap);
  }

  public boolean isEmpty()
  {
    return this.mutationMap.isEmpty();
  }
}


